﻿Namespace nHibernate.Models
    Public Class OrderModel
        Public Sub New()
        End Sub

        Public Overridable Property UniqueID As String
            Get
                Return CInt(DokumentArt).ToString + "-" + OrderID.ToString
            End Get
            Set(value As String)

            End Set
        End Property
        Public Overridable Property OrderID As Integer
        Public Overridable Property DokumentArt As Integer
        Public Overridable Property Datum As Date
        Public Overridable Property LinkedToCredit As String
        Public Overridable Property LinkedToOffer As String
        Public Overridable Property LinkedToInvoice As String
        Public Overridable Property LinkedToDelivery As String
        Public Overridable Property LinkedFromCredit As String
        Public Overridable Property LinkedFromOffer As String
        Public Overridable Property LinkedFromInvoice As String
        Public Overridable Property LinkedFromDelivery As String
        Public Overridable Property ReductionAmount As Decimal
        Public Overridable Property ReductionText As String
        Public Overridable Property OrderText As String
        Public Overridable Property Rebate As Decimal
        Public Overridable Property TextBefore As String

        Public Overridable Property LastEditBy As nHibernate.Models.UserPreviewModel
        Public Overridable Property DeliveryAddress As nHibernate.Models.DeliveryAddressModel
        Public Overridable Property PaymentMethode As nHibernate.Models.PaymentMethodeModel
        Public Overridable Property OrderedArticles() As IList(Of nHibernate.Models.OrderedArticleModel)
        Public Overridable Property Customer As nHibernate.Models.CustomerModel

        Public Overridable Property CustomerNamePreview As String
        Public Overridable Property TotalValueAmount As Decimal '-> Preview
        Public Overridable Property TotalValueCurrency As Integer

        Public Overridable Function ToOrder() As Order
            Dim t = New Order With {.Datum = New Datum(Datum), .LinkedFromCredit = LinkedFromCredit, .LinkedFromDelivery = LinkedFromDelivery, .LinkedFromInvoice = LinkedFromInvoice, .LinkedFromOffer = LinkedFromOffer, _
                                   .LinkedToCredit = LinkedToCredit, .LinkedToDelivery = LinkedToDelivery, .LinkedToInvoice = LinkedToInvoice, .LinkedToOffer = LinkedToOffer, .Reduction = New Money(ReductionAmount, TotalValueCurrency), _
                                   .ReductionText = ReductionText, .Rebate = Rebate, .PaymentMethode = PaymentMethode.ToPaymentMethode, .Customer = Customer.ToCustomer, .DokumentArt = DokumentArt, .OrderID = OrderID, .TextBefore = TextBefore, .LastEditBy = LastEditBy}
            If DeliveryAddress IsNot Nothing Then
                t.DeliveryAddress = DeliveryAddress.ToAddress
            End If
            For Each item In OrderedArticles
                t.OrderedArticles.Add(item.ToOrderedArticle)
            Next
            Return t
        End Function

        Public Shared Function FromOrder(o As Order) As OrderModel
            Dim t As New OrderModel With {.OrderID = o.OrderID, .DokumentArt = o.DokumentArt, .Datum = o.Datum.ToDate, .LinkedFromCredit = o.LinkedFromCredit, .LinkedFromDelivery = o.LinkedFromDelivery, .LinkedFromInvoice = o.LinkedFromInvoice, _
                                          .LinkedFromOffer = o.LinkedFromOffer, .LinkedToCredit = o.LinkedToCredit, .LinkedToDelivery = o.LinkedToDelivery, .LinkedToInvoice = o.LinkedToInvoice, .LinkedToOffer = o.LinkedToOffer, _
                                          .ReductionAmount = o.Reduction.Amount, .ReductionText = o.ReductionText, .OrderText = o.OrderText, .Rebate = o.Rebate, .CustomerNamePreview = o.Customer.DisplayName, .TotalValueAmount = o.TotalValue.Amount, .TextBefore = o.TextBefore, _
                                          .TotalValueCurrency = o.Reduction.Currency, .Customer = CustomerModel.FromCustomer(o.Customer), .DeliveryAddress = DeliveryAddressModel.FromAddress(o.DeliveryAddress, o.UniqueID), .PaymentMethode = PaymentMethodeModel.FromPaymentMethode(o.PaymentMethode), .LastEditBy = o.LastEditBy}
            Dim li As New List(Of nHibernate.Models.OrderedArticleModel)
            For Each item In o.OrderedArticles
                li.Add(OrderedArticleModel.FromOrderedArticle(item, t.UniqueID))
            Next
            t.OrderedArticles = li
            Return t
        End Function
    End Class
End Namespace
